<?php
/************************
* Simple News Engine 0.2.3
* author: Ragnaar <ragnaar@km.ru>
* home: http://code.google.com/p/sne-news
* date: 08.06.2011
*********************/
define('_VALID_SNE', '1');
define('SNE_PATH', dirname(__FILE__)); //определяем путь

require_once SNE_PATH."/inc/config.php";
require_once SNE_PATH."/inc/lng/".$language.".php";
require_once SNE_PATH."/inc/connect.php";
require_once SNE_PATH."/inc/pager.class.php";
require_once SNE_PATH."/inc/functions.php";

//шаблоны
$pub_header_tpl = SNE_PATH."/inc/tpl/pub_header.tpl";
$pub_footer_tpl = SNE_PATH."/inc/tpl/pub_footer.tpl";

$modes = array('page', 'news', 'include'); // все режимы

if (isset($inc)){ // позволяем включать режим include локально, на случай если allow_url_fopen и allow_url_include установлены в off
	$mode = 'include';
} else {
    if (isset($_GET['mode']) && in_array($_GET['mode'], $modes)) {
        $mode = $_GET['mode'];
    } else {
        $mode = 'page'; //по умолчанию - страница
    }
}

/************************
* Page mode
*********************/
if ($mode == 'page'){

    if (isset($_GET['p']) && $_GET['p'] > 0){ //определяем страницу
        $page = (int)$_GET['p'];
    } else {
        $page = 1;
    }

    $start = ($page - 1) * $pg_news_num;

    $tot = mysql_query(prefix("SELECT count(*) FROM `{prefix}news` WHERE `pub` = 1;", $dbprefix));
    $query = prefix("SELECT `id`, `title`, SUBSTRING(`text`,1, LOCATE('<hr id=\"cut\">', `text`) -1) AS `text`, `cut`, `comment`, `image`, `date`, `pub` FROM `{prefix}news` WHERE `pub` = 1 ORDER BY `id` DESC LIMIT $start, $pg_news_num", $dbprefix) ;

    $thm = mysql_query($query);

    if($tot && $thm){
        $total = mysql_fetch_array($tot);
        $total_records = $total['count(*)']; //Всего найдено
    }

    $title = _SNE_NEWS; //текст заголовка
    $header = "";
    include $pub_header_tpl; //инклюдим заголовок

    while($row = mysql_fetch_object($thm)) { //вывод блока с новостью
    	echo "<div class='newsitem'>\n";
        echo "\t<h3><a href='?mode=news&amp;id=".$row->id."'>".$row->title."</a></h3>\n";
        echo "\t<span class='nicons ndate'>".hdate($row->date, $month_names, $date_template)."</span>\n";
        if ($row->image){
            $img_num = mysql_query(prefix("SELECT count(*) FROM `{prefix}images` WHERE `news_id` = $row->id AND `pub` = 1", $dbprefix));
            $img_num = mysql_fetch_array($img_num);
            $img_num = $img_num['count(*)'];
            if ($img_num > 0){
                echo "<span class='nicons nimage'><a href='".$newsPath."?mode=news&amp;id=".$row->id."#nimages'>".$img_num._SNE_IMAGES."</a></span>\n";
            }
        }
        if ($row->comment){
            $comm_num = mysql_query(prefix("SELECT `num`, `socnet` FROM `{prefix}comments` WHERE `nid` = $row->id and `socnet` = '".$socialnet."'", $dbprefix));
            $comm_num = mysql_fetch_array($comm_num);
            if ($comm_num['num'] > 0){
                echo "<span class='nicons ncomm'><a href='".$newsPath."?mode=news&amp;id=".$row->id."#comments'>".$comm_num['num']._SNE_COMMENTS."</a></span>\n";
            }
        }
        echo "\t<div>".$row->text."</div>\n";
        if ($row->cut){
            echo "\t<div class='more'><a href='".$newsPath."?mode=news&amp;id=".$row->id."'>"._SNE_MORE."</a></div>\n";
        }
    	echo "</div><!-- /.newsitem -->\n";
    }
    if ($total_records > $pg_news_num){
    	pager($total_records, $pg_news_num, $page, '?p=');
    }

    include $pub_footer_tpl; //инклюдим подвал
}
/************************
* News mode
*********************/
if ($mode == 'news'){

    if (isset($_GET['id'])) {
        $id = (int)$_GET['id'];
		
		require_once SNE_PATH."/inc/js.php";

        $rows = mysql_query(prefix("SELECT * FROM `{prefix}news` WHERE `id` = $id AND `pub` = 1", $dbprefix));
        $row = mysql_fetch_object($rows);

        if ($row){
            $title = $row->title; //текст заголовка
            $header = $jqueryJs.$imgboxJs; // JavaScript
            if ($socialization == 1){
                $header = $header.$socialnetInitJs;
            }
            include $pub_header_tpl; //инклюдим заголовок

    	    echo "<div class='newsitem'>\n";
            echo "<h3>".$row->title."</h3>\n";
            echo "\t<span class='nicons ndate'>".hdate($row->date, $month_names, $date_template)."</span>\n";
            if ($row->image){
                $img_num = mysql_query(prefix("SELECT count(*) FROM `{prefix}images` WHERE `news_id` = $row->id AND `pub` = 1", $dbprefix));
                $img_num = mysql_fetch_array($img_num);
                $img_num = $img_num['count(*)'];
                if ($img_num > 0){
                    echo "<span class='nicons nimage'><a href='?mode=news&amp;id=".$row->id."#nimages'>".$img_num._SNE_IMAGES."</a></span>\n";
                }
            }
            if ($row->comment){
                $comm_num = mysql_query(prefix("SELECT `num` FROM `{prefix}comments` WHERE `nid` = $row->id AND `socnet` = '".$socialnet."'", $dbprefix));
                $comm_num = mysql_fetch_array($comm_num);
                if ($comm_num['num'] > 0){
                    echo "<span class='nicons ncomm'><a href='".$newsPath."?mode=news&amp;id=".$row->id."#comments'>".$comm_num['num']._SNE_COMMENTS."</a></span>\n";
                }
            }
            if ($socialization == 1){
                echo $socialnetLikeJs;
            }
            echo "<div>".str_replace('<hr id="cut">', '', $row->text)."</div>\n";
            echo "<a href='javascript:history.back()'>"._SNE_BACK."</a>";
    	    echo "</div><!-- /.newsitem -->\n";

            if ($row->image){  //если ть картинки - выводим их таблицей под текстом
                $imgs = mysql_query(prefix("SELECT * FROM {prefix}images WHERE news_id=".$id." AND pub=1;", $dbprefix));
                $num_imgs = mysql_num_rows($imgs); //считаем количество картинок

                $trow = $num_imgs / $tcell; //количество строк

                echo "<div id='nimages'>\n<table>\n";
                for($i = 0; $i < $trow; $i++){
                    echo "\t<tr>\n";
                    for($h = 0; $h < $tcell; $h++){
                	    $img = mysql_fetch_object($imgs);
                	    if (!$img){
                	        break;
                	    }
                        echo "\t\t<td><a href='./image/".$img->name."' class='imgbox' id='img".$img->id."'><img src='./thumb/".$img->name."' alt='".$img->title."' title='".$img->description."' /></a></td>\n";
                    }
                    echo "\t</tr>\n";
                }
                echo "</table>\n</div>\n";
            }
            if ($socialization == 1){ //выводим  блок комментариев
                echo $socialnetJs;
            }
        } else {
    	    $title = "<p>"._SNE_NOT_FOUND."</p>"; //текст заголовка
            include $pub_header_tpl; //инклюдим заголовок

            echo "<p>"._SNE_NOT_FOUND."</p>";
            echo "<a href='javascript:history.back()'>"._SNE_BACK."</a>";
        }
    } else {
    	$title = "<p>"._SNE_NOT_FOUND."</p>"; //текст заголовка
        include $pub_header_tpl; //инклюдим заголовок

        echo "<p>"._SNE_NOT_FOUND."</p>";
        echo "<a href='javascript:history.back()'>"._SNE_BACK."</a>";
    }
    include $pub_footer_tpl; //инклюдим подвал
}
/************************
* Include mode
*********************/
if ($mode == 'include'){

    $query = prefix("SELECT `id`, `title`, SUBSTRING(`text`,1, LOCATE('<hr id=\"cut\">', `text` )-1) AS `text`, `cut`, `comment`, `image`, `date`, `pub` FROM `{prefix}news` WHERE `pub` = 1 ORDER BY `id` DESC LIMIT 0, $inc_news_num", $dbprefix);

    $thm = mysql_query($query);

    while($row = mysql_fetch_object($thm)) {
    	echo "<div class='newsitem'>\n";
        echo "<h3><a href='".$newsPath."?mode=news&amp;id=".$row->id."'>".$row->title."</a></h3>\n";
        echo "\t<span class='nicons ndate'>".hdate($row->date, $month_names, $date_template)."</span>\n";
        if ($row->image){
            $img_num = mysql_query(prefix("SELECT count(*) FROM `{prefix}images` WHERE `news_id` = $row->id AND `pub` = 1", $dbprefix));
            $img_num = mysql_fetch_array($img_num);
            $img_num = $img_num['count(*)'];
            if ($img_num > 0){
                echo "<span class='nicons nimage'><a href='".$newsPath."?mode=news&amp;id=".$row->id."#nimages'>".$img_num._SNE_IMAGES."</a></span>\n";
            }
        }
        if ($row->comment){
            $comm_num = mysql_query(prefix("SELECT `num` FROM `{prefix}comments` WHERE `nid` = $row->id AND `socnet` = '".$socialnet."'", $dbprefix));
            $comm_num = mysql_fetch_array($comm_num);
            if ($comm_num['num'] > 0){
                echo "<span class='nicons ncomm'><a href='".$newsPath."?mode=news&amp;id=".$row->id."#comments'>".$comm_num['num']._SNE_COMMENTS."</a></span>\n";
            }
        }

        echo "\t<div>".$row->text."</div>\n";
        if ($row->cut){
            echo "\t<div class='more'><a href='".$newsPath."?mode=news&amp;id=".$row->id."'>"._SNE_MORE."</a></div>\n";
        }
    	echo "</div><!-- /.newsitem -->\n";
    }
    echo "<a href='".$newsPath."'>"._SNE_ALL_NEWS."</a>";
}
?>
